#!/usr/bin/env python

import pygame
from pygame.locals import *
import random
import sys

import pygamesvg
from pgu import gui

import debug
import image
import rule
import text
import tile
import unit
import widget

from debug import *

TITLE    = 'Boop'
S_WIDTH  = 480
S_HEIGHT = 270
FPS      = 30

def main():
  pygame.init()
  
  screen = pygame.display.set_mode((S_WIDTH, S_HEIGHT), HWACCEL|DOUBLEBUF)
  screen.fill((0, 0, 0))
  
  t_size = (30, 30)
  
  
  text.load_font('vlgothic.ttf', 'dontcare', size=14, set_as_default=True)
  text.load_font('vlgothic.ttf', 'vl_small', size=12)
  
  cursor = unit.load_cursor(cursor_normal=image.SmoothAlpha('map_cursor.svg', 
                                frames=60, start=1.0, stop=0.5, size=t_size),
                            cursor_attack=image.Still(
                                image.svg_filename_to_surface('crosshair.svg', size=t_size)))
  h_tile = image.Still(image.svg_filename_to_surface('highlight_tile.svg', 
                      size=t_size, alpha=0.5))
  
  grass = image.svg_file_to_surface(file('t_grass.svg'), size=t_size)
  water = image.svg_file_to_surface(file('t_water.svg'), size=t_size)
  forest = pygame.image.load('t_forest.png')
  forest2 = pygame.image.load('t_forest_2.png')
  mountain = pygame.image.load('t_mountain.png')
  
  water_surfaces = tile.load_water_surfaces('beach.*png', base_dir='.')
  water_surfaces[tile.DIR_NONE] = water
  
  tt_grass = tile.TileType(name='grass', 
                          long_name='Grass', 
                          short_name='Grss', 
                          surface=grass, 
                          defense_value=1)
  tt_forest = tile.TileType(name='forest', long_name='Forest', short_name='Frst', surface=forest, defense_value=2)
  tt_forest_2 = tile.TileType(name='forest', long_name='Forest', short_name='Frst', surface=forest2, defense_value=2)
  tt_mountain = tile.TileType(name='mountain', long_name='Mountain', short_name='Mtn', surface=mountain, defense_value=4)
  tt_water = tile.CSensitiveTileType(name='water', long_name='Sea', short_name='Sea', surfaces=water_surfaces,
                                    terrain_type=tile.TERRAIN_WATER, by_name=False)
  
  
  tile_engine = tile.TileEngine(tile_wh=t_size)
  g = tile_engine.register_tile_type(tt_grass, id=0)
  f = tile_engine.register_tile_type(tt_forest)
  f2 = tile_engine.register_tile_type(tt_forest_2)
  m = tile_engine.register_tile_type(tt_mountain)
  w = tile_engine.register_tile_type(tt_water, id=1)
  
  map = [[w, w, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, w, w],
         [w, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, w],
         [w, g, g, g, f, g, g, w, g, g, g, g, w, g, g, g, g, g, g],
         [g, g, g, f, f2, g, g, w, w, w, w, w, w, g, g, g, g, g, g],
         [m, g, f2, g, f, g, g, g, w, w, w, g, g, g, g, g, g, g, g],
         [g, m, g, m, m, w, g, g, w, w, w, w, g, g, g, g, g, g, g],
         [m, g, g, f, m, w, w, w, w, w, w, w, g, g, g, g, g, g, g],
         [g, g, g, g, g, g, g, w, w, w, w, w, w, g, g, g, g, g, g],
         [g, g, g, g, g, w, g, f2, f, f, f2, g, w, w, g, g, g, g, g],
         [g, g, g, g, w, w, w, g, g, g, g, g, w, g, g, g, g, g, g],
         [g, g, g, g, g, w, w, g, g, g, g, w, w, g, g, g, g, g, g],
         [g, g, g, g, g, g, g, g, g, g, g, g, w, g, g, g, g, g, g],
         [g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g],
         [g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g],
         [g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g],
         [g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g],
         [g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g],
         [w, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, w]
         
        ]
  
  tile_engine.load_map(map)
  
  rule_engine = rule.RuleEngine()
  
  rule_engine.register_rule(rule.Rule(rule.TYPE_PLACEMENT,
                            unit_class=unit.CLASS_TREADS,
                            terrain_type=tile.TERRAIN_WATER,
                            can_place=False))
  
  rule_engine.register_rule(rule.Rule(rule.TYPE_PLACEMENT,
                            unit_class=unit.CLASS_TREADS,
                            tile_name='mountain',
                            can_place=False))
  
  rule_engine.register_rule(rule.Rule(rule.TYPE_MOVEMENT, 
                            unit_class=unit.CLASS_TREADS,
                            terrain_type=tile.TERRAIN_WATER,
                            cost=unit.MOV_INF))
  
  rule_engine.register_rule(rule.Rule(rule.TYPE_MOVEMENT, 
                            unit_class=unit.CLASS_TREADS,
                            tile_name='mountain',
                            cost=unit.MOV_INF))
  
  rule_engine.register_rule(rule.Rule(rule.TYPE_MOVEMENT, 
                            unit_class=unit.CLASS_TREADS,
                            tile_name='forest',
                            cost=2))
  
  b_anim = image.Still(image.svg_filename_to_surface('metal_ball.svg', size=t_size))
  
  ball = unit.UnitType('Ball', unit.CLASS_TREADS,
                        max_hp=100, max_mp=4, max_fuel=90,
                        state_normal=b_anim)
  tank = unit.UnitType('Tank', unit.CLASS_TREADS,
                        max_hp=10, max_mp=5, max_fuel=70,
                        state_normal=image.Still(pygame.image.load('tank.png')))
  
  b_unit    = unit.Unit(ball)
  b_unit2   = unit.Unit(ball)
  
  t_unit1 = unit.Unit(tank)
  
  unit_engine = unit.UnitEngine(tile_engine, rule_engine)
  unit_engine.set_mv_highlight_tile(h_tile)
  unit_engine.set_arrow(unit.load_arrow('arrow', size=t_size))
  unit_engine.set_cursor(cursor)
  
  unit_engine.place_unit(b_unit, (2, 5))
  unit_engine.place_unit(b_unit2, (1, 6))
  unit_engine.place_unit(t_unit1, (0, 5))
  
  app = gui.App()
  
  #container = gui.Container(align=-1, valign=-1)
  container = widget.OverlayContainer((S_WIDTH, S_HEIGHT), align=-1, valign=-1)
  
  viewport = widget.MapViewport((S_WIDTH, S_HEIGHT), unit_engine)
  viewport.register_cursor_listener(unit_engine)
  
  info_overlay = widget.InfoOverlay(tile_engine, unit_engine, 
                              font=text.get_font('vl_small'),
                              defense_indicator=image.svg_filename_to_surface('star.svg', size=(16, 16)),
                              fuel_indicator=image.svg_filename_to_surface('fuel.svg', size=(13, 16)),
                              hp_indicator=image.svg_filename_to_surface('heart.svg', size=(16, 16)))
  viewport.register_cursor_listener(info_overlay)
  ov_w, ov_h = info_overlay.resize()
  
  
  
  #l.connect(gui.CLICK, open_close, None)
  
  container.add(viewport, 0, 0)
  container.add(info_overlay, S_WIDTH - ov_w, S_HEIGHT - ov_h)
  app.init(container)
  
  
  clock = pygame.time.Clock()
  
  while True:
    for e in pygame.event.get():
      if e.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
      app.event(e)
    app.paint(screen)
    pygame.display.flip()
    debug('%d msecs since previous tick.' % (clock.tick(FPS)))
  
  
if __name__ == "__main__":
  main()
